package com.yanqu.road.dao.impl.player.cricket;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.cricket.CricketData;
import com.yanqu.road.entity.cricket.CricketProperty;
import com.yanqu.road.entity.cricket.CricketSkill;
import com.yanqu.road.entity.cricket.CricketTalent;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CricketDaoImpl extends TempDataObjectDao<CricketData> {

    public Map<Long, Map<Integer, CricketData>> getSomeFishDataMap(List<Long> userIdList) {
        Map<Long,Map<Integer, CricketData>> map = new ConcurrentHashMap<>();
        if(userIdList.size() == 0){
            return map;
        }
        String getSql = "select * from t_u_cricket where userId in("+ StringUtils.listToString(userIdList,",") +")";
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getSql);
            rs = call.executeQuery();
            while (rs.next()) {
                CricketData data = parse(rs);
                if(!map.containsKey(data.getUserId())){
                    map.put(data.getUserId(),new ConcurrentHashMap<>());
                }
                map.get(data.getUserId()).put(data.getCricketId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public Map<Integer, CricketData> getCricketDataMap(long userId) {
        Map<Integer, CricketData> dataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_cricket where userId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                CricketData data = parse(rs);
                dataMap.put(data.getCricketId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    private static String insertCricketDataSql = "INSERT INTO t_u_cricket(userId,cricketId,quality,title,littleTitle,cricketTalent,cricketProperty,addProperty,attackSkillList,initSkillList,skillList,matchTimes,lastMatchTime,battleTimes,battleWinTimes,currentSeasonId,currentSeasonBattleTimes,currentSeasonBattleWinTimes,equipTimes,equipBuyTimes,equipPatronsId,rewardNum) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(CricketData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCricketId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getQuality()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTitle()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLittleTitle()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getCricketTalent())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getCricketProperty())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getAddProperty())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getAttackSkillList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getInitSkillList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getSkillList())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getMatchTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastMatchTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBattleTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBattleWinTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCurrentSeasonId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCurrentSeasonBattleTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCurrentSeasonBattleWinTimes()));

        para.put(i++, new DbParameter(Types.INTEGER, data.getEquipTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEquipBuyTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEquipPatronsId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRewardNum()));
        if (getDb().executeNoneQuery(insertCricketDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateCricketDataSql = "update `t_u_cricket` set `quality`=?,`title`=?,`littleTitle`=?,`cricketTalent`=?,`cricketProperty`=?,`addProperty`=?,`attackSkillList`=?,`initSkillList`=?,`skillList`=?,`matchTimes`=?,`lastMatchTime`=?,`battleTimes`=?,`battleWinTimes`=?,`currentSeasonId`=?,`currentSeasonBattleTimes`=?,`currentSeasonBattleWinTimes`=?,`equipTimes`=?,`equipBuyTimes`=?,`equipPatronsId`=?,`rewardNum`=? where `userId`=? and `cricketId`=?";

    @Override
    protected boolean updateData(CricketData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getQuality()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTitle()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLittleTitle()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getCricketTalent())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getCricketProperty())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getAddProperty())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getAttackSkillList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getInitSkillList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getSkillList())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getMatchTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastMatchTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBattleTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBattleWinTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCurrentSeasonId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCurrentSeasonBattleTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCurrentSeasonBattleWinTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEquipTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEquipBuyTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEquipPatronsId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRewardNum()));


        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCricketId()));
        if (getDb().executeNoneQuery(updateCricketDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected CricketData parseData(ResultSet rs) throws SQLException {
        CricketData data = new CricketData();
        data.setUserId(rs.getLong("userId"));
        data.setCricketId(rs.getInt("cricketId"));
        data.setQuality(rs.getInt("quality"));
        data.setTitle(rs.getInt("title"));
        data.setLittleTitle(rs.getInt("littleTitle"));
        data.setCricketProperty(JSON.parseObject(rs.getString("cricketProperty"), new TypeReference<CricketProperty>() {
        }));
        if (data.getCricketProperty() == null) {
            data.setCricketProperty(new CricketProperty());
        }
        data.setAddProperty(JSON.parseObject(rs.getString("addProperty"), new TypeReference<CricketProperty>() {
        }));
        if (data.getAddProperty() == null) {
            data.setAddProperty(new CricketProperty());
        }
        data.setCricketTalent(JSON.parseObject(rs.getString("cricketTalent"), new TypeReference<CricketTalent>() {
        }));
        data.setAttackSkillList(JSON.parseObject(rs.getString("attackSkillList"), new TypeReference<List<CricketSkill>>() {
        }));
        data.setInitSkillList(JSON.parseObject(rs.getString("initSkillList"), new TypeReference<List<CricketSkill>>() {
        }));
        data.setSkillList(JSON.parseObject(rs.getString("skillList"), new TypeReference<List<CricketSkill>>() {
        }));
        data.setMatchTimes(rs.getInt("matchTimes"));
        data.setLastMatchTime(rs.getLong("lastMatchTime"));
        data.setBattleTimes(rs.getInt("battleTimes"));
        data.setBattleWinTimes(rs.getInt("battleWinTimes"));
        data.setCurrentSeasonId(rs.getInt("currentSeasonId"));
        data.setCurrentSeasonBattleTimes(rs.getInt("currentSeasonBattleTimes"));
        data.setCurrentSeasonBattleWinTimes(rs.getInt("currentSeasonBattleWinTimes"));

        data.setEquipTimes(rs.getInt("equipTimes"));
        data.setEquipBuyTimes(rs.getInt("equipBuyTimes"));
        data.setEquipPatronsId(rs.getInt("equipPatronsId"));
        data.setRewardNum(rs.getInt("rewardNum"));
        return data;
    }
}
